#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/12/8 20:21
# @Author  : lizhen
# @Site    : 
# @File    : gc.py
# @Software: PyCharm
import gc
import sys


class ClassA:
    def __init__(self):
        print('init object born,id:%s' % str(hex(id(self))))

    def __del__(self):
        print('del object born,id:%s' % str(hex(id(self))))


class GarbageCollect:
    def get_ref_count(self):
        word = 'hello'
        print(sys.getrefcount(word))

    def get_gc_garbage(self):
        gc.set_debug(gc.DEBUG_LEAK)
        c1 = ClassA()
        c2 = ClassA()
        c1.t = c2
        c2.t = c1
        del c1
        del c2
        print(gc.garbage)
        print(gc.collect())
        print(gc.garbage)

    def get_gc_count(self):
        print(gc.get_count())
        a = ClassA()
        print(gc.get_count())
        del a
        print(gc.get_count())

    def get_gc_threshold(self):
        print(gc.get_threshold())

    def get_gc_del_unreachable(self):
        gc.set_debug(gc.DEBUG_LEAK)
        a = ClassA()
        b = ClassA()
        a.next = b
        b.prev = a
        print(gc.collect())
        del a
        del b
        print(gc.collect())


def main():
    gc = GarbageCollect()
    # gc.get_ref_count()
    # gc.get_gc_garbage()
    # gc.get_gc_count()
    # gc.get_gc_threshold()
    gc.get_gc_del_unreachable()


if __name__ == '__main__':
    main()
